//#include "GeomDoc.h"
//#include "GeomDoc.h"
// GeomDoc.cpp : implementation of the CGeomDoc class
//

#include "stdafx.h"
#include "AnyApplication.h"
#include "GeomDoc.h"
#include "AnyDocument.h"
#include "AnyApplication.h"
#include "GeomApp.h"
#include "GeomDocTree.h"
#include "FileExporter.h"
#include "ExporterFactory.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CGeomDoc

IMPLEMENT_DYNCREATE(CGeomDoc, CDocument)

BEGIN_MESSAGE_MAP(CGeomDoc, CDocument)
END_MESSAGE_MAP()


// CGeomDoc construction/destruction

CGeomDoc::CGeomDoc()
{
	// TODO: add one-time construction code here
	m_pAnyDoc = NULL;// GetAnyApp().CreateDocument();
}

CGeomDoc::~CGeomDoc()
{
	delete m_pAnyDoc;
}

BOOL CGeomDoc::OnNewDocument()
{
	//if (!CDocument::OnNewDocument())
	//	return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	m_pAnyDoc =  GetAnyApp().CreateDocument();
	m_pAnyDoc->AddListener(GetGeomApp().GetDocTree());
	m_pAnyDoc->NewDocument(GetTitle());
	return TRUE;
}

void CGeomDoc::OnActiveDocument(bool bActive)
{
	if(m_pAnyDoc)
		m_pAnyDoc->SetActive(bActive);
}


// CGeomDoc serialization

void CGeomDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}


// CGeomDoc diagnostics

#ifdef _DEBUG
void CGeomDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CGeomDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CGeomDoc commands

void CGeomDoc::OnCloseDocument()
{
	// TODO: Add your specialized code here and/or call the base class
	m_pAnyDoc->CloseDocument();
	CDocument::OnCloseDocument();
}

BOOL CGeomDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
	// TODO: Add your specialized code here and/or call the base class

	return CDocument::OnSaveDocument(lpszPathName);
}

void CGeomDoc::OnFileImport(const CString& szImpotrerGUID, CAnyView* pAnyView)
{

}

void CGeomDoc::OnFileExport(const CString& szExporterGUID, CAnyView* pAnyView)
{
	CFileExporter* pExporter = GetFileExporterFactory().Create(szExporterGUID);
	if(pExporter)
	{
		CXportInfo& xportInfo = pExporter->GetInfo();
		xportInfo.szFileName = GetTitle() + xportInfo.szFileExt;
		CFileDialog dlg(FALSE, xportInfo.szFileName, NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
			xportInfo.szDescription);
		if(IDOK == dlg.DoModal())
		{
			xportInfo.szFullPath = dlg.GetPathName();
			xportInfo.szFileName = dlg.GetFileName();
			xportInfo.SetDocument(m_pAnyDoc);
			xportInfo.SetView(pAnyView);

			pExporter->Export();
		}
	}
}